var axios = require('axios');
const wxConfig = require('../utils/wxConfig.js');

const Common = {
    resJson: function (code, message, data) {
        let obj = {
            code: code,
            message: message,
            data: data
        }
        return JSON.stringify(obj)
    },

    getIPAdress(IP) {
        return new Promise((resolve, reject) => {
            axios.get(`https://api.ipplus360.com/ip/geo/v1/city/?key=wvGDikuxhVf4QAKe1gaWySwp4t3YEzqGmEe4XgTpMXSWRLW1kZ244FFnPlvaTmJ0&ip=${IP}`)
                .then(res => {
                    resolve(res.data)
                })
                .catch(error => {
                    reject(error)
                })
        })
    },

    getWeather(Postal) {
        return new Promise((resolve, reject) => {
            axios.get(`https://api.map.baidu.com/weather/v1/?district_id=${Postal}&data_type=all&ak=YFOz0Ua8hqtnV5CklygbnO1CPkviEJeC`)
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(error)
                })
        })
    },

    jscode2session(code) {
        return this.APIRequest('get', `https://api.weixin.qq.com/sns/jscode2session?appid=${wxConfig.appid}&secret=${wxConfig.appsecret}&js_code=${code}&grant_type=authorization_code`,null)
    },

    getAccessToken() {
        return new Promise((resolve, reject) => {
            axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${wxConfig.appid}&secret=${wxConfig.appsecret}`)

                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(error)
                })
        })
    },

    getApiQuota() {
        return this.APIRequest('post', 'https://api.weixin.qq.com/cgi-bin/openapi/quota/get', { access_token: '86_GBk_6G9dPNhVo7WoS13t3gNgllxpMTT45o-NZSP0Ox6oUmBEjDocGP7brKelrFh7mT3xiLox_--rjtwHMRTI2tEiyfIaONA1mUEdSF2A5u9Sxd8P7bgfIS0S4xcGEPcAEASQBs' })
    },



    APIRequest(method, url, data) {
        return new Promise((resolve, reject) => {
            axios({ method: method, url: url, data: data })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(error)
                })
        })
    }
}

module.exports = Common;